package com.example.onlinemarket.graphics;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.appcompat.widget.AppCompatEditText;

public class DrawableClickableEditText extends AppCompatEditText {

    private OnDrawableStartListener mStartListener;
    private OnDrawableEndListener mEndListener;
    private OnDrawableListener drawableListener;
    final int DRAWABLE_START = 0;
    final int DRAWABLE_TOP = 1;
    final int DRAWABLE_END = 2;
    final int DRAWABLE_BOTTOM = 3;

    public DrawableClickableEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public DrawableClickableEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DrawableClickableEditText(Context context) {
        super(context);
    }

    public void setOnDrawableStartListener(OnDrawableStartListener listener) {
        this.mStartListener = listener;
    }

    public void setOnDrawableEndListener(OnDrawableEndListener listener) {
        this.mEndListener = listener;
    }

    public void setOnDrawableClickListener(OnDrawableListener listener) {
        this.drawableListener = listener;
    }

    public interface OnDrawableListener {
        void onDrawableStartClick(View view);
        void onDrawableEndClick(View view);
    }

    public interface OnDrawableStartListener {
        void onDrawableStartClick(View view);
    }

    public interface OnDrawableEndListener {
        void onDrawableEndClick(View view);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (mEndListener != null) {
                Drawable drawableRight = getCompoundDrawables()[DRAWABLE_END];
                if (drawableRight != null && event.getRawX() >= (getRight() - drawableRight.getBounds().width())) {
                    mEndListener.onDrawableEndClick(this);
                    return true;
                }
            }
            if (drawableListener != null) {
                Drawable drawableEnd = getCompoundDrawables()[DRAWABLE_END];
                if (drawableEnd != null && event.getRawX() >= (getRight() - drawableEnd.getBounds().width())) {
                    drawableListener.onDrawableEndClick(this);
                    return true;
                }
                Drawable drawableStart = getCompoundDrawables()[DRAWABLE_START];
                if (drawableStart != null && event.getRawX() <= (getLeft() + drawableStart.getBounds().width())) {
                    drawableListener.onDrawableStartClick(this);
                    return true;
                }
            }
            if (mStartListener != null) {
                Drawable drawableLeft = getCompoundDrawables()[DRAWABLE_START];
                if (drawableLeft != null && event.getRawX() <= (getLeft() + drawableLeft.getBounds().width())) {
                    mStartListener.onDrawableStartClick(this);
                    return true;
                }
            }
        }

        return super.onTouchEvent(event);
    }

}

